﻿using Sasa.QualityTools.CodeAnalysis.Core.Properties;
using System;
using System.Collections.Generic;

namespace Sasa.QualityTools.CodeAnalysis.Core.Services.CodeAnalysis
{
    /// <summary>
    /// This class represents an error that occurs while FxCopCmd.exe is running. 
    /// </summary>
    internal class FxCopExecutionException : Exception
    {
        internal FxCopExecutionException(int errorCode)
            : base(GetErrorMessage(errorCode))
        {
        }

        /// <summary>
        /// Initializes a new instance that contains the specified error message and exception that is the cause of this exception.
        /// </summary>
        /// <param name="message">The error message.</param>
        /// <param name="innerException">The exception that is cause of this exception.</param>
        internal FxCopExecutionException(string message, Exception innerException)
            : base(message, innerException)
        {
        }

        /// <summary>
        /// Get the FxCop error message.
        /// </summary>
        /// <param name="errorCode">The error code that was returned by FxCopCmd.exe process.</param>
        /// <returns>The FxCop error message.</returns>
        private static string GetErrorMessage(int errorCode)
        {
            FxCopError error = (FxCopError)Enum.ToObject(typeof(FxCopError), errorCode);

            List<string> messages = new List<string>();
            if (error.HasFlag(FxCopError.AnalysisError))
            {
                messages.Add(Resources.FxCopReturnsAnalysisError);
            }
            if (error.HasFlag(FxCopError.RuleExceptions))
            {
                messages.Add(Resources.FxCopReturnsRuleExceptionError);
            }
            if (error.HasFlag(FxCopError.ProjectLoadError))
            {
               messages.Add(Resources.FxCopReturnsProjectLoadError);
            }
            if (error.HasFlag(FxCopError.AssemblyLoadError))
            {
                messages.Add( Resources.FxCopReturnsAssemblyLoadError);
            }
            if (error.HasFlag(FxCopError.RuleLibraryLoadError))
            {
                messages.Add(Resources.FxCopReturnsRuleLibraryLoadError);
            }
            if (error.HasFlag(FxCopError.ImportReportLoadError))
            {
             messages.Add(Resources.FxCopReturnsImportReportLoadError);
            }
            if (error.HasFlag(FxCopError.OutputError))
            {
                messages.Add( Resources.FxCopReturnsOutputError);
            }
            if (error.HasFlag(FxCopError.CommandLineSwitchError))
            {
                messages.Add( Resources.FxCopReturnsCommnadLineSwitchError);
            }
            if (error.HasFlag(FxCopError.InitializationError))
            {
                messages.Add( Resources.FxCopReturnsInitializationError);
            }
            if (error.HasFlag(FxCopError.AssemblyReferencesError))
            {
                messages.Add(Resources.FxCopReturnsAssemblyReferencesError);
            }
            if (error.HasFlag(FxCopError.UnknownError))
            {
                messages.Add(Resources.FxCopReturnsUnknownError);
            }

            return String.Join(Environment.NewLine, messages.ToArray());
        }
    }
}
